package com.ruoyi.neusoft.service.impl;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.neusoft.domain.FileUpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.neusoft.mapper.AiIdentifyModelCheckTMapper;
import com.ruoyi.neusoft.domain.AiIdentifyModelCheckT;
import com.ruoyi.neusoft.service.IAiIdentifyModelCheckTService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.web.multipart.MultipartFile;

/**
 * 智能AI模型校验信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-17
 */
@Service
public class AiIdentifyModelCheckTServiceImpl implements IAiIdentifyModelCheckTService {
    @Autowired
    private AiIdentifyModelCheckTMapper aiIdentifyModelCheckTMapper;

    /**
     * 查询智能AI模型校验信息
     *
     * @param checkId 智能AI模型校验信息主键
     * @return 智能AI模型校验信息
     */
    @Override
    public AiIdentifyModelCheckT selectAiIdentifyModelCheckTByCheckId(Long checkId) {
        return aiIdentifyModelCheckTMapper.selectAiIdentifyModelCheckTByCheckId(checkId);
    }

    /**
     * 查询智能AI模型校验信息列表
     *
     * @param aiIdentifyModelCheckT 智能AI模型校验信息
     * @return 智能AI模型校验信息
     */
    @Override
    public List<AiIdentifyModelCheckT> selectAiIdentifyModelCheckTList(AiIdentifyModelCheckT aiIdentifyModelCheckT) {
        return aiIdentifyModelCheckTMapper.selectAiIdentifyModelCheckTList(aiIdentifyModelCheckT);
    }

    /**
     * 新增智能AI模型校验信息
     *
     * @param aiIdentifyModelCheckT 智能AI模型校验信息
     * @return 结果
     */
    @Override
    public int insertAiIdentifyModelCheckT(AiIdentifyModelCheckT aiIdentifyModelCheckT) {
        aiIdentifyModelCheckT.setCreateTime(DateUtils.getNowDate());
        return aiIdentifyModelCheckTMapper.insertAiIdentifyModelCheckT(aiIdentifyModelCheckT);
    }

    /**
     * 修改智能AI模型校验信息
     *
     * @param aiIdentifyModelCheckT 智能AI模型校验信息
     * @return 结果
     */
    @Override
    public int updateAiIdentifyModelCheckT(AiIdentifyModelCheckT aiIdentifyModelCheckT) {
        return aiIdentifyModelCheckTMapper.updateAiIdentifyModelCheckT(aiIdentifyModelCheckT);
    }

    /**
     * 批量删除智能AI模型校验信息
     *
     * @param checkIds 需要删除的智能AI模型校验信息主键
     * @return 结果
     */
    @Override
    public int deleteAiIdentifyModelCheckTByCheckIds(String checkIds) {
        return aiIdentifyModelCheckTMapper.deleteAiIdentifyModelCheckTByCheckIds(Convert.toStrArray(checkIds));
    }

    /**
     * 删除智能AI模型校验信息信息
     *
     * @param checkId 智能AI模型校验信息主键
     * @return 结果
     */
    @Override
    public int deleteAiIdentifyModelCheckTByCheckId(Long checkId) {
        return aiIdentifyModelCheckTMapper.deleteAiIdentifyModelCheckTByCheckId(checkId);
    }

    @Override
    public String upload(FileUpVo fileUpVo) {

        Integer index = fileUpVo.getIndex();
        Long modelId = fileUpVo.getModelId();
        MultipartFile file = fileUpVo.getFile();
        String url = "";
        String sql = "";
        try {
            url = "http://localhost" + FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InvalidExtensionException e) {
            throw new RuntimeException(e);
        }

        if (1 == index) {
            sql = "update ai_identify_model_check_t set check_resource_path = '" + url + "' where model_id =" + modelId;
            aiIdentifyModelCheckTMapper.doUpdateSql(sql);
        } else {
            sql = "update ai_identify_model_check_t set checked_resource_path = '" + url + "' where model_id =" + modelId;
            aiIdentifyModelCheckTMapper.doUpdateSql(sql);
        }

        return url;
    }
}
